package com.zidian.teacher.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.bean.TagsBean;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import com.zidian.teacher.adapter.LayoutAdapter;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;

import teacher.mingmingde.com.recycle.RecyclerViewPager;


/**
 * Created by Administrator on 2016/9/4 0004.
 */
public class EvaluateListActivity extends ToolBarActivity {

    protected RecyclerViewPager mRecyclerView;
    private TextView sumbit;
    private LayoutAdapter adapter;
    private List<TagsBean.DataBean> data = new ArrayList<>();
    private String byEvaluatePersonId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_evaluate_tags);
        setContentView(R.layout.activity_evaluate_tag_demo);
        setTitle("标签评价");
        byEvaluatePersonId = getIntent().getStringExtra("byEvaluatePersonId");
        getData();
        initViewPager();
    }

    protected void initViewPager() {
        mRecyclerView = (RecyclerViewPager) findViewById(R.id.viewpager);
        sumbit = (TextView) findViewById(R.id.toolbar_sumbit);
        LinearLayoutManager layout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
                false);
        mRecyclerView.setLayoutManager(layout);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLongClickable(true);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {
//                updateState(scrollState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int i, int i2) {
//                mPositionText.setText("First: " + mRecyclerViewPager.getFirstVisiblePosition());
                int childCount = mRecyclerView.getChildCount();
                int width = mRecyclerView.getChildAt(0).getWidth();
                int padding = (mRecyclerView.getWidth() - width) / 2;
//                mCountText.setText("Count: " + childCount);

                for (int j = 0; j < childCount; j++) {
                    View v = recyclerView.getChildAt(j);
                    //往左 从 padding 到 -(v.getWidth()-padding) 的过程中，由大到小
                    float rate = 0;
                    ;
                    if (v.getLeft() <= padding) {
                        if (v.getLeft() >= padding - v.getWidth()) {
                            rate = (padding - v.getLeft()) * 1f / v.getWidth();
                        } else {
                            rate = 1;
                        }
                        v.setScaleY(1 - rate * 0.1f);
                        v.setScaleX(1 - rate * 0.1f);

                    } else {
                        //往右 从 padding 到 recyclerView.getWidth()-padding 的过程中，由大到小
                        if (v.getLeft() <= recyclerView.getWidth() - padding) {
                            rate = (recyclerView.getWidth() - padding - v.getLeft()) * 1f / v.getWidth();
                        }
                        v.setScaleY(0.9f + rate * 0.1f);
                        v.setScaleX(0.9f + rate * 0.1f);
                    }
                }
            }
        });
        mRecyclerView.addOnPageChangedListener(new RecyclerViewPager.OnPageChangedListener() {
            @Override
            public void OnPageChanged(int oldPosition, int newPosition) {
                Log.d("test", "oldPosition:" + oldPosition + " newPosition:" + newPosition);
            }
        });

        mRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (mRecyclerView.getChildCount() < 3) {
                    if (mRecyclerView.getChildAt(1) != null) {
                        if (mRecyclerView.getCurrentPosition() == 0) {
                            View v1 = mRecyclerView.getChildAt(1);
                            v1.setScaleY(0.9f);
                            v1.setScaleX(0.9f);
                        } else {
                            View v1 = mRecyclerView.getChildAt(0);
                            v1.setScaleY(0.9f);
                            v1.setScaleX(0.9f);
                        }
                    }
                } else {
                    if (mRecyclerView.getChildAt(0) != null) {
                        View v0 = mRecyclerView.getChildAt(0);
                        v0.setScaleY(0.9f);
                        v0.setScaleX(0.9f);
                    }
                    if (mRecyclerView.getChildAt(2) != null) {
                        View v2 = mRecyclerView.getChildAt(2);
                        v2.setScaleY(0.9f);
                        v2.setScaleX(0.9f);
                    }
                }

            }
        });


        sumbit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (getReback() == null) {
                    ToastUtil.showToast(EvaluateListActivity.this, "请左右滑动选择标签");
                } else {
//                    ToastUtil.showToast(EvaluateListActivity.this, getReback());
                    evaluateTag(getReback());
                }


            }
        });


    }

    public void getData() {

        String loginUrl = Constant.URL_TEACHER + "Label/selectAllLabelName";
        HashMap<String, String> map = new HashMap<>();
        map.put("packageName", "教评");
        map.put("operatorId", GetUserInfo.getTeacherId(context));
        map.put("operatorType", "教师");
        map.put("token", GetUserInfo.getToken(context));
        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("E", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {

                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    Log.i("response",response.toString());
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        ToastUtil.showToast(context, "获取问题列表失败！");
                        return;
                    } else {
//                        String json = "{\"message\":\"查询成功\",\"data\":[{\"packageName\":\"教评\",\"threeindexname\":\"人格成长\",\"indexName\":\"教学态度\",\"label\":[{\"labelName\":\"自带爹妈光环\"},{\"labelName\":\"他是谁\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"内容量\",\"indexName\":\"教学过程评价\",\"label\":[{\"labelName\":\"能够消化\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"学业进展\",\"indexName\":\"教学态度\",\"label\":[{\"labelName\":\"认真严谨\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"思维与创新能力提升\",\"indexName\":\"教学效果评价\",\"label\":[{\"labelName\":\"只是想想而已\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"教学方法多样性\",\"indexName\":\"教学过程评价\",\"label\":[{\"labelName\":\"丰富多彩\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"教学方法有效性\",\"indexName\":\"教学过程评价\",\"label\":[{\"labelName\":\"秒懂666\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"深浅程度\",\"indexName\":\"教学过程评价\",\"label\":[{\"labelName\":\"包罗万象\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"着装仪表\",\"indexName\":\"教学态度\",\"label\":[{\"labelName\":\"完美\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"精神面貌\",\"indexName\":\"教学态度\",\"label\":[{\"labelName\":\"神采奕奕\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"课程目标1\",\"indexName\":\"教学效果评价\",\"label\":[{\"labelName\":\"课程目标1实现的不好\"}]},{\"packageName\":\"教评\",\"threeindexname\":\"课程目标2\",\"indexName\":\"教学效果评价\",\"label\":[{   \"labelName\": \"得心应手驾轻就熟\"},{ \"labelName\": \"原地踏步\"}]}],\"code\":200}\n";
                        Gson gson = new Gson();
                        TagsBean tags = gson.fromJson(response, TagsBean.class);
                        data.addAll(tags.getData());
                        adapter = new LayoutAdapter(context, data);
                        mRecyclerView.setAdapter(adapter);
                    }

                } catch (JSONException e) {

                }

                Log.e("T", response);


            }
        });


    }

    public String getReback() {
        Map<Integer, String> map = new HashMap<>();
        map = adapter.getSlect();
        String result = "";

        result = map.get(0);

        for (int i = 1; i < data.size(); i++) {

            if (map.get(i) == null) {
                result = null;
                break;
            }
            result += "," + map.get(i);
        }
        return result;
    }

    private void evaluateTag(String evaluateLabel) {


        String loginUrl = Constant.URL_TEACHER + "ToEvaluateOthers/evaluationColleagues";
        HashMap<String, String> map = new HashMap<>();
        map.put("colleagueId", GetUserInfo.getTeacherId(context));
        map.put("byEvaluatePersonId", byEvaluatePersonId);
        map.put("evaluateLabel", evaluateLabel);
        map.put("token", GetUserInfo.getToken(context));

        OkHttpUtils.post().url(loginUrl).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.showToast(context, e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {

                Log.e("T", response);
                try {
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");

                    if (resultCode != 200) {
                        ToastUtil.showToast(context, jsonObject.getString("message"));
                        return;
                    } else {
                        ToastUtil.showToast(context, "评价成功");
                        finish();
                    }

                } catch (JSONException e) {

                }


            }
        });

    }


}
